import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwind from 'tailwindcss'
import autoprefixer from 'autoprefixer'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
    // 加载当前环境的.env文件变量​
    const env = loadEnv(mode, process.cwd())
    // 根据环境变量动态设置base​
    const base = env.VITE_BASE_PATH
    return {
        plugins: [
            vue(),
            tailwind(),
            AutoImport({
                resolvers: [ElementPlusResolver()]
            }),
            Components({
                resolvers: [ElementPlusResolver()]
            })
        ],
        base, // 设置基础路径
        build: {
            terserOptions: {
                compress: {
                    drop_console: true // 生产环境移除console语句
                }
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: `@use "@/style/element.scss" as *;`,
                    api: 'modern-compiler', // or 'modern'
                    // 忽略特定警告
                    quietDeps: true
                }
            },
            postcss: {
                plugins: [
                    tailwind(),
                    autoprefixer()
                    // 其他postcss插件
                ]
            }
        },

        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        },
        server: {
            sourcemap: true,
            hmr: true,
            host: 'dev.tijian.com',
            port: 3002,
            open: 'http://dev.tijian.com'
        }
    }
})
